package cn.yutong.system.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import cn.yutong.framework.RestResponse;
import cn.yutong.framework.util.TimeTools;
import cn.yutong.system.entity.EnvIndicators;
import cn.yutong.system.service.IEnvIndicatorsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @author lnexin@aliyun.com
 */
@RestController
@Api(tags = "气象数据接口说明")
@RequestMapping({ "/env" })
public class EnvIndicatorsController {

	@Autowired
	IEnvIndicatorsService envIndicatorsService;

	@GetMapping({ "/conditions" })
	@ApiOperation("气象数据(列表)查询接口")
	public RestResponse queryByConditions(
			@ApiParam(required = true, name = "staTime", value = "开始筛选时间:2018-01-01") @RequestParam String staTime,
			@ApiParam(required = true, name = "endTime", value = "结束筛选时间:2018-01-01") @RequestParam String endTime)
			throws ParseException {
		EntityWrapper<EnvIndicators> ew = new EntityWrapper<EnvIndicators>();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		long staDate = format.parse(staTime).getTime() / 1000L;
		long endDate = format.parse(endTime).getTime() / 1000L;
		staDate = TimeTools.convertToMinOrMax(staDate, true, false);
		endDate = TimeTools.convertToMinOrMax(endDate, false, true);
		ew.between("timestamp", Long.valueOf(staDate), Long.valueOf(endDate));
		List<EnvIndicators> indicators = this.envIndicatorsService.selectList(ew);

		return RestResponse.success("查询成功", indicators);
	}

	@GetMapping({ "/dimension" })
	@ApiOperation("分维度条件查询接口")
	public RestResponse queryByDimension(
			@ApiParam(required = true, name = "staTime", value = "开始筛选时间:2018-01-01") @RequestParam String staTime,
			@ApiParam(required = true, name = "endTime", value = "结束筛选时间:2018-01-01") @RequestParam String endTime)
			throws ParseException {
		EntityWrapper<EnvIndicators> ew = new EntityWrapper<EnvIndicators>();

		long staDate = TimeTools.strDateParse2Long(staTime, "yyyy-MM-dd") / 1000L;
		long endDate = TimeTools.strDateParse2Long(endTime, "yyyy-MM-dd") / 1000L;
		// SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		// long staDate = format.parse(staTime).getTime() / 1000L;
		// long endDate = format.parse(endTime).getTime() / 1000L;
		staDate = TimeTools.convertToMinOrMax(staDate, true, false);
		endDate = TimeTools.convertToMinOrMax(endDate, false, true);
		ew.between("timestamp", Long.valueOf(staDate), Long.valueOf(endDate));
		List<EnvIndicators> indicators = this.envIndicatorsService.selectList(ew);

		List<Map<Object, Object>> responseData = getDimensionResponse(indicators);
		for (Map<Object, Object> responseMap : responseData) {
			responseMap.put("total", Integer.valueOf(indicators.size()));
			responseMap.put("count", Integer.valueOf(indicators.size()));
			responseMap.put("type", "lines");
			responseMap.put("label", "时间," + responseMap.get("itemTitle").toString().split("变化")[0]);
		}
		return RestResponse.success("查询成功", responseData);
	}

	private List<Map<Object, Object>> getDimensionResponse(List<EnvIndicators> indicators) {
		List<Map<Object, Object>> responseData = Lists.newArrayList();

		HashMap<Object, Object> atmosphereTempResult = Maps.newLinkedHashMap();
		HashMap<Object, Object> atmosphereHumidityResult = Maps.newLinkedHashMap();
		HashMap<Object, Object> soilHumidityResult = Maps.newLinkedHashMap();
		HashMap<Object, Object> soilTempResult = Maps.newLinkedHashMap();
		HashMap<Object, Object> rainfallResult = Maps.newLinkedHashMap();
		HashMap<Object, Object> rainfallDayResult = Maps.newLinkedHashMap();
		HashMap<Object, Object> windSpeedResult = Maps.newLinkedHashMap();
		HashMap<Object, Object> windDirectionResult = Maps.newLinkedHashMap();

		Map<String, Object> atmosphereTempItem = Maps.newLinkedHashMap();
		Map<String, Object> atmosphereHumidityItem = Maps.newLinkedHashMap();
		Map<String, Object> soilHumidityItem = Maps.newLinkedHashMap();
		Map<String, Object> soilTempItem = Maps.newLinkedHashMap();
		Map<String, Object> rainfallItem = Maps.newLinkedHashMap();
		Map<String, Object> rainfallDayItem = Maps.newLinkedHashMap();
		Map<String, Object> windSpeedItem = Maps.newLinkedHashMap();
		Map<String, Object> windDirectionItem = Maps.newLinkedHashMap();
		for (EnvIndicators env : indicators) {
			String dateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(env.getTime());
			atmosphereTempItem.put(dateTime, env.getAtmosphereTemp());
			atmosphereHumidityItem.put(dateTime, env.getAtmosphereHumidity());
			soilHumidityItem.put(dateTime, env.getSoilHumidity());
			soilTempItem.put(dateTime, env.getSoilTemp());
			rainfallItem.put(dateTime, env.getRainfall());
			rainfallDayItem.put(dateTime, env.getRainfallDay());
			windSpeedItem.put(dateTime, env.getWindSpeed());
			windDirectionItem.put(dateTime, env.getWindDirection());
		}
		atmosphereTempResult.put("itemDataList", ImmutableList
				.of(ImmutableMap.of("label", "大气温度", "total", Integer.valueOf(100), "datas", atmosphereTempItem)));
		atmosphereTempResult.put("itemTitle", "大气温度变化趋势");
		responseData.add(atmosphereTempResult);

		atmosphereHumidityResult.put("itemDataList", ImmutableList
				.of(ImmutableMap.of("label", "大气湿度", "total", Integer.valueOf(100), "datas", atmosphereHumidityItem)));
		atmosphereHumidityResult.put("itemTitle", "大气湿度变化趋势");
		responseData.add(atmosphereHumidityResult);

		soilHumidityResult.put("itemDataList", ImmutableList
				.of(ImmutableMap.of("label", "土壤湿度", "total", Integer.valueOf(100), "datas", soilHumidityItem)));
		soilHumidityResult.put("itemTitle", "土壤湿度变化趋势");
		responseData.add(soilHumidityResult);

		soilTempResult.put("itemDataList", ImmutableList
				.of(ImmutableMap.of("label", "土壤湿度", "total", Integer.valueOf(100), "datas", soilTempItem)));
		soilTempResult.put("itemTitle", "土壤温度变化趋势");
		responseData.add(soilTempResult);

		rainfallResult.put("itemDataList", ImmutableList
				.of(ImmutableMap.of("label", "降雨量", "total", Integer.valueOf(100), "datas", rainfallItem)));
		rainfallResult.put("itemTitle", "降雨量变化趋势");
		responseData.add(rainfallResult);

		rainfallDayResult.put("itemDataList", ImmutableList
				.of(ImmutableMap.of("label", "日降雨量", "total", Integer.valueOf(100), "datas", rainfallDayItem)));
		rainfallDayResult.put("itemTitle", "日降雨量变化趋势");
		responseData.add(rainfallDayResult);

		windSpeedResult.put("itemDataList", ImmutableList
				.of(ImmutableMap.of("label", "风速", "total", Integer.valueOf(100), "datas", windSpeedItem)));
		windSpeedResult.put("itemTitle", "风速变化趋势");
		responseData.add(windSpeedResult);

		windDirectionResult.put("itemDataList", ImmutableList
				.of(ImmutableMap.of("label", "风向", "total", Integer.valueOf(100), "datas", windDirectionItem)));
		windDirectionResult.put("itemTitle", "风向变化趋势");
		responseData.add(windDirectionResult);
		return responseData;
	}

	@GetMapping({ "/{id}" })
	@ApiOperation("查询单条数据")
	public EnvIndicators get(@PathVariable("id") Integer id) {
		return (EnvIndicators) this.envIndicatorsService.selectById(id);
	}

	@RequestMapping(value = { "/convert" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ApiIgnore
	public RestResponse queryByConditions() {
		EntityWrapper<EnvIndicators> ew = new EntityWrapper<EnvIndicators>();
		List<EnvIndicators> indicators = this.envIndicatorsService.selectList(ew);
		for (EnvIndicators indicator : indicators) {
			Date time = indicator.getTime();
			Long longTime = Long.valueOf(time.getTime());
			System.out.println(time + " - " + (int) (longTime.longValue() / 1000L));
			indicator.setTimestamp(Integer.valueOf((int) (longTime.longValue() / 1000L)));
			this.envIndicatorsService.updateAllColumnById(indicator);
		}
		return RestResponse.success("查询成功", indicators);
	}
}